package com.lwc.cfdns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwc.cfdns.common.Assert;
import com.lwc.cfdns.constants.Constants;
import com.lwc.cfdns.enums.ResultCode;
import com.lwc.cfdns.exception.BadException;
import com.lwc.cfdns.pojo.bo.system.DictBO;
import com.lwc.cfdns.pojo.dto.system.DictDTO;
import com.lwc.cfdns.pojo.entity.SystemDict;
import com.lwc.cfdns.pojo.vo.system.DictVO;
import com.lwc.cfdns.service.SystemDictService;
import com.lwc.cfdns.mapper.SystemDictMapper;
import com.lwc.cfdns.utils.ConvertUtils;
import com.lwc.cfdns.utils.RedisUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author spring
 * @description 针对表【cfdns_system_dict(字典类型表)】的数据库操作Service实现
 * @createDate 2023-05-28 20:42:57
 */
@Service
public class SystemDictServiceImpl extends ServiceImpl<SystemDictMapper, SystemDict>
        implements SystemDictService {
    @Resource
    private RedisUtils redisUtils;

    /**
     * 创建
     *
     * @param dictDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 21:01
     */
    @Override
    public Map<String, Object> create(DictDTO dictDTO) {
        Long count = this.baseMapper.selectCount(new LambdaQueryWrapper<SystemDict>().eq(SystemDict::getMarks, dictDTO.getMarks()));
        if (count > 0) {
            throw new BadException("字典标识已存在!");
        }
        SystemDict systemDict = new SystemDict();
        BeanUtils.copyProperties(dictDTO, systemDict);
        this.baseMapper.insert(systemDict);
        Map<String, Object> map = new HashMap<>();
        map.put("id", systemDict.getId());
        return map;
    }

    /**
     * 修改
     *
     * @param dictDTO
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 21:21
     */
    @Override
    public Map<String, Object> update(DictDTO dictDTO) {
        SystemDict systemDict = this.baseMapper.selectById(dictDTO.getId());
        Assert.notNull(systemDict, ResultCode.NOT_DATA_FOUNT);
        BeanUtils.copyProperties(dictDTO, systemDict);
        int i = this.baseMapper.updateById(systemDict);
        Map<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 详情
     *
     * @param id
     * @return DictVO
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 21:30
     */
    @Override
    public DictVO view(Long id) {
        SystemDict systemDict = this.baseMapper.selectById(id);
        Assert.notNull(systemDict, ResultCode.NOT_DATA_FOUNT);
        DictVO dictVO = new DictVO();
        BeanUtils.copyProperties(systemDict, dictVO);
        return dictVO;
    }

    /**
     * 删除(支持批理)
     *
     * @param ids
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 21:47
     */
    @Override
    public Map<String, Object> delete(String ids) {
        int i = 0;
        if (ids.contains(",")) {
            i = this.baseMapper.delete(new LambdaQueryWrapper<SystemDict>().in(SystemDict::getId, ConvertUtils.toLongArray(ids)));
        } else {
            i = this.baseMapper.deleteById(ConvertUtils.toLong(ids));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("result", i);
        return map;
    }

    /**
     * 列表
     *
     * @param dictBO
     * @return IPage<SystemDict>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/28 21:47
     */
    @Override
    public IPage<SystemDict> lists(DictBO dictBO) {
        Page<SystemDict> page = this.baseMapper.selectPage(dictBO.toPage(), dictBO.toQueryWrapper());
        // 按OV返回
        page.convert(s -> {
            DictVO vo = new DictVO();
            BeanUtils.copyProperties(s, vo);
            return vo;
        });
        return page;
    }

    /**
     * 刷新缓存
     *
     * @param marks
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/29 15:48:33
     */
    @Override
    public Map<String, Object> refreshCache(String marks) {
        Boolean res = redisUtils.del(Constants.dictPrefix + marks);
        Map<String, Object> map = new HashMap<>();
        map.put("result", res ? 1 : 0);
        return map;
    }

    /**
     * 刷新缓存
     *
     * @return Map<String, Object>
     * @throws
     * @version 1.0.0
     * @author luwc
     * @time 2023/5/29 15:48:33
     */
    @Override
    public Map<String, Object> refreshCache() {
        List<Object> marks = this.baseMapper.selectObjs(new LambdaQueryWrapper<SystemDict>().select(SystemDict::getMarks));
        List<Object> keys = new ArrayList<>();
        for (Object mark : marks) {
            // 增加前缀
            keys.add(Constants.dictPrefix + mark);
        }
        Long res = redisUtils.del(keys);
        Map<String, Object> map = new HashMap<>();
        map.put("result", res);
        return map;
    }
}




